package com.ymttest.business.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.shop2cn.app.liveseller.parameter.*;
import com.shop2cn.app.liveseller.service.*;
import com.shop2cn.iapi.livebuyer.parameter.*;
import com.shop2cn.iapi.livebuyer.service.*;
import com.ymatou.iapi.prodstock.parameter.AgentProductPo;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.livesellerShop2cn.*;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.livesellerShop2cn.LiveRoomShop2cnWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/************************************************************************************
 * @File name :LiveRoomCallService.java
 * @Author : zhouyi
 * @Date : 2017年10月30日
 * @Copyright : 洋码头
 ************************************************************************************/
public class LiveRoomShop2cnCallService {
//	live.antiKey=5dd1d3d9c83ddb31c67772a526cbbe06
//			live.apiKey=5dd1d3d9c83ddb31c67772a526cbbe06
	/**
	 * 直播推流key
	 */
	public static String LiveAntiKey = "5dd1d3d9c83ddb31c67772a526cbbe06";

	/**
	 * 直播API鉴权key
	 */
	public static String LiveApiKey = "5dd1d3d9c83ddb31c67772a526cbbe06";
	public static String AppId = "1301003690";
	// 20181024域名变更 不再用 12808.liveplay.myqcloud.com
	public static String BizId = "72055";
	public static String PlayDomain = "vplay.ymatou.com";
	public static String PlaySellerDomain = "vtestplay.shop2cn.com";
//	public static String ImSdkAppId = "1400301043";
	public static String ImSdkAppId = "1400299618";
	public static String ImAccountType = "18457";

	// 待确认
	// public static String SdkAppId = "1400046801";
	/**
	 * 根据上级商品id和自己的商户id查自己的商品id
	 * @param parentProductId
	 * @param userid
	 * @return
	 */
	public static String getMyProductId(String parentProductId, Integer userid) {
		List<AgentProductPo> list = new YmtProductsIWapper().selectAgentProductByParentProductIdAndUserId(parentProductId, userid);
		if(list!=null&&list.size()>0) {
			return list.get(0).getProductId();
		}else {
			return null;
		}
	}

	/**
	 * 创建直播bean 初始化默认Createsellerlive参数
	 * 
	 * @return
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static CreateSellerLiveBean initCreatesellerliveBean()
			throws InterruptedException, ClientProtocolException, IOException {
		CreateSellerLiveBean createsellerliveBean = new CreateSellerLiveBean();
		LiveRoom livedto = new LiveRoom();
		// 默认码头直播
		livedto.setLiveType(0);
		livedto.setCountryId(1);
		livedto.setLiveDesc("直播简介");
		livedto.setEndTime(YMTDateUtil.getTimeBySecond(120 * 60).toString());
		// 第三方创建较慢 使用模拟数据
		// livedto.setGroupId(CreateGroup("AutoTestGroup" +
		// TestDataManager.getRandomNum(1)));
		livedto.setGroupId(String.valueOf(YMTDateUtil.getDate().getTime()));
		livedto.setStartTime(YMTDateUtil.getTimeBySecond(30 * 60).toString());
		livedto.setLatitude("31." + TestDataManager.getRandomNum(4));
		livedto.setLongitude("100." + TestDataManager.getRandomNum(4));
		livedto.setLiveAddress("live地址" + TestDataManager.getRandomNum(4));
		livedto.setLiveCover("http://img.ymatou.com/app/flag/circle/America.png");
		livedto.setLiveTitle("直播标题" + TestDataManager.getRandomNum(18));
		livedto.setShoppingAddress("ShoppingAddress:" + TestDataManager.getRandomNum(4));
		livedto.setPreVideoHeight(100);
		livedto.setPreVideoLength(1500);
		livedto.setPreVideoPic("http://img.ymatou.com/app/flag/circle/France.png");
		livedto.setPreVideoWidth(200);
		livedto.setPreVideoUrl("http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4");
//		livedto.setSellerName(uinfo.getLoginId());
//		livedto.setUserId(uinfo.getUserId());
		livedto.setLiveCategoryId(1);
		livedto.setBuyerShow(true);
		createsellerliveBean.setLiveDto(livedto);
		createsellerliveBean.setRequestId(TestDataManager.getRandomUUID().toString());
		return createsellerliveBean;
	}

	public static LiveRoom CreateSellerLive(CreateSellerLiveBean clive, Integer sellerid)
			throws ClientProtocolException, IOException, InterruptedException {
		return CreateSellerLive(clive, true, sellerid);
	}

	/**
	 * 创建直播
	 * 
	 * @param createsellerliveBean
	 * @param autoclose
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException
	 */
	private static LiveRoom CreateSellerLive(CreateSellerLiveBean createsellerliveBean, boolean autoclose,
			Integer sellerid) throws ClientProtocolException, IOException, InterruptedException {
		Logger.comment("创建直播:autoclose:" + autoclose);
		if (autoclose) {
			AutoCloseLiveRoomByUserId(sellerid);
		}
		String token = AuthCallServiceV2.createToken(sellerid, "app");
		CreateSellerLiveCall createsellerliveCall = new CreateSellerLiveCall();
		createsellerliveCall.setData(false, createsellerliveBean);
		String head = "accesstoken=" + token;
		createsellerliveCall.addHeader("ymt-pars", head);
		createsellerliveCall.callService();
		int liveid = createsellerliveCall.getJsonObject().get("liveId").getAsInt();
		LiveRoom dblive = new LiveRoomShop2cnWapper().selectLiveRoomById(liveid);
		return dblive;
	}

	/**
	 * 关闭直播 慎用
	 * 
	 * @param liveid
	 * @param userid
	 * @param type   7 - 系统自动关闭直播，8 - 关闭直播，9 - 结束直播，10 - 强制关停<br>
	 *               7需要结束时间超过当前时间且断流<br>
	 *               8需要没有推流过<br>
	 *               9需要推流过而且当前时间超过开始时间<br>
	 *               10<br>
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void CloseSellerLive(Integer liveid, Integer sellerid, Integer type)
			throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":关闭直播:type:" + type);
		String token = AuthCallServiceV2.createToken(sellerid, "app");
		CloseSellerLiveBean closesellerliveBean = new CloseSellerLiveBean();
		CloseSellerLiveCall closesellerliveCall = new CloseSellerLiveCall();
		if (type.equals(8)) {
			closesellerliveBean.setLiveId(liveid);
			closesellerliveBean.setCloseType("1");
			closesellerliveCall.setData(false, closesellerliveBean);
		} else if (type.equals(9)) {
			closesellerliveBean.setLiveId(liveid);
			closesellerliveBean.setCloseType("2");
			closesellerliveCall.setData(false, closesellerliveBean);
		}
		String head = "accesstoken=" + token;
		closesellerliveCall.addHeader("ymt-pars", head);
		closesellerliveCall.callService();
	}

//	/**
//	 * 强制系统关闭 会修改直播时间和状态--目前没有
//	 * 
//	 * @param liveid
//	 * @throws IOException
//	 * @throws ClientProtocolException
//	 * @throws InterruptedException
//	 */
//	public static void SysCloseLive(Integer liveid) throws ClientProtocolException, IOException, InterruptedException {
//		LiveRoomShop2cnWapper liveRoomWapper = new LiveRoomShop2cnWapper();
//		liveRoomWapper.updateLiveRoomTime(YMTDateUtil.getBeforeOrNextMunite(-100), YMTDateUtil.getBeforeOrNextMunite(-50),YMTDateUtil.getBeforeOrNextMunite(-50), liveid);
//		if (liveRoomWapper.selectLiveRoomById(liveid).getLiveStatus() == 2) {
//			liveRoomWapper.updateLiveRoomPushStatus(liveid, 3);
//		}
//		SysCloseLiveCall syscloseliveCall = new SysCloseLiveCall();
//		SysCloseLiveBean syscloseliveBean = new SysCloseLiveBean();
//		syscloseliveCall.setData(syscloseliveBean);
//		syscloseliveCall.callService();
//		Thread.sleep(3000);
//	}
	/**
	 * 删除直播
	 * 
	 * @param liveid
	 * @param uid
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void RemoveSellerLive(Integer liveid, Integer sellerid) throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":删除直播:uid:" + sellerid);
		String token = AuthCallServiceV2.createToken(sellerid, "app");
		RemoveSellerLiveBean removesellerliveBean = new RemoveSellerLiveBean();
		RemoveSellerLiveCall removesellerliveCall = new RemoveSellerLiveCall();
		removesellerliveBean.setLiveId(liveid);
		removesellerliveCall.setData(removesellerliveBean);
		String head = "accesstoken=" + token;
		removesellerliveCall.addHeader("ymt-pars", head);
		removesellerliveCall.callService();
	}

	/**
	 * 排序直播
	 * 
	 * @param liveid
	 * @param uid
	 * @param ProductIdList
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void SortLiveProduct(Integer liveid, Integer sellerid, List<String> ProductIdList)
			throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":主排序直播商品:uid:" + sellerid);
		String token = AuthCallServiceV2.createToken(sellerid, "app");
		SortLiveProductBean sortliveproductBean = new SortLiveProductBean();
		SortLiveProductCall sortliveproductCall = new SortLiveProductCall();
		sortliveproductBean.setUserId(sellerid);
		sortliveproductBean.setLiveId(liveid);
		sortliveproductBean.setProductIdList(ProductIdList);
		sortliveproductCall.setData(sortliveproductBean);
		String head = "accesstoken=" + token;
		sortliveproductCall.addHeader("ymt-pars", head);
		sortliveproductCall.callService();
	}

	/**
	 * 重载 批量添加
	 * 
	 * @param liveid
	 * @param userid
	 * @param ProductIdList
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void AddProductsToLive(Integer liveid, Integer sellerid, List<String> ProductIdList)
			throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":添加商品:ProductIdList:" + ProductIdList);
		String token = AuthCallServiceV2.createToken(sellerid, "app");
		AddProductsToLiveBean addproductstoliveBean = new AddProductsToLiveBean();
		AddProductsToLiveCall addproductstoliveCall = new AddProductsToLiveCall();
		addproductstoliveBean.setLiveId(liveid);
		addproductstoliveBean.setProductIdList(ProductIdList);
		addproductstoliveCall.setData(false, addproductstoliveBean);
		String head = "accesstoken=" + token;
		addproductstoliveCall.addHeader("ymt-pars", head);
		addproductstoliveCall.callService();
	}

	/**
	 * 新增继承直播间
	 * 
	 * @param liveid
	 * @param InheritUserId 代理id
	 * @param ParentShopId  上级商家id(代理渠道商)
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void AddLiveToShop(Integer liveid, Integer InheritUserId, Integer ParentShopId)
			throws ClientProtocolException, IOException {
		String token = AuthCallServiceV2.createToken(InheritUserId, "app");
		AddLiveToShopBean addlivetoshopBean = new AddLiveToShopBean();
		AddLiveToShopCall addlivetoshopCall = new AddLiveToShopCall();
		addlivetoshopBean.setLiveRoomId(liveid);
		addlivetoshopBean.setInheritUserId(InheritUserId);
		addlivetoshopBean.setParentShopId(ParentShopId);
		addlivetoshopCall.setData(addlivetoshopBean);
		String head = "accesstoken=" + token;
		addlivetoshopCall.addHeader("ymt-pars", head);
		addlivetoshopCall.callService();
	}

	/**
	 * 移除直播继承
	 * 
	 * @param liveid
	 * @param InheritUserId
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void RemoveLive(Integer liveid, Integer InheritUserId) throws ClientProtocolException, IOException {
		String token = AuthCallServiceV2.createToken(InheritUserId, "app");
		RemoveLiveBean RemoveLiveBean = new RemoveLiveBean();
		RemoveLiveCall RemoveLiveCall = new RemoveLiveCall();
		RemoveLiveBean.setLiveRoomId(liveid);
		RemoveLiveBean.setInheritUserId(InheritUserId);
		RemoveLiveCall.setData(RemoveLiveBean);
		String head = "accesstoken=" + token;
		RemoveLiveCall.addHeader("ymt-pars", head);
		RemoveLiveCall.callService();
	}

	/**
	 * 设置公告
	 * 
	 * @param liveid
	 * @param content
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void SaveLiveNotice(Integer liveid, String content) throws ClientProtocolException, IOException {
		SaveLiveNoticeBean savelivenoticeBean = new SaveLiveNoticeBean();
		SaveLiveNoticeCall savelivenoticeCall = new SaveLiveNoticeCall();
		savelivenoticeBean.setContent(content);
		savelivenoticeBean.setLiveId(liveid);
		savelivenoticeBean.setOperator("autotest");
		savelivenoticeCall.setData(savelivenoticeBean);
		savelivenoticeCall.callService();
	}

	/**
	 * 设置直播间是否允许联播
	 * 
	 * @param liveid
	 * @param LiveInherit 0.不允许1.允许
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void SetisLiveinherit(Integer sellerid, Integer liveid, Integer LiveInherit)
			throws ClientProtocolException, IOException {
		String token = AuthCallServiceV2.createToken(sellerid, "app");
		SetIsLiveInheritBean setisliveinheritBean = new SetIsLiveInheritBean();
		SetIsLiveInheritCall setisliveinheritCall = new SetIsLiveInheritCall();
		setisliveinheritBean.setLiveInherit(LiveInherit);
		setisliveinheritBean.setLiveRoomId(liveid);
		String head = "accesstoken=" + token;
		setisliveinheritCall.addHeader("ymt-pars", head);
		setisliveinheritCall.setData(setisliveinheritBean);
		setisliveinheritCall.callService();
	}

	/**
	 * 重播地址
	 * 
	 * @param id
	 * @param file_id
	 * @return
	 */
	public static ReplayNotifyBean initReplayNotifyBean(Integer id, String file_id) {
		ReplayNotifyBean rb = new ReplayNotifyBean();

		MetaData metaData = new MetaData();
		Drm drm = new Drm();
		List<Trancode> processTaskList = new ArrayList<Trancode>();
		ProcedureStateChanged pc = new ProcedureStateChanged();
		rb.setEventType("ProcedureStateChanged");// 需要校验
		rb.setVersion("4.0");
		rb.setData(pc);
		pc.setErrCode(0);// 需要校验
		pc.setFileId(file_id);// 需要校验
		pc.setMessage("");
		pc.setMetaData(metaData);
		pc.setStatus("FINISH");
		pc.setDrm(drm);
		pc.setProcessTaskList(processTaskList);
		Trancode e = new Trancode();
		e.setTaskType("Transcode");// 需要校验
		e.setErrCode(0);// 需要校验
		e.setStatus("SUCCESS");// 需要校验
		TrancodeInPut input = new TrancodeInPut();
		input.setDefinition(220);// 需要校验
		e.setInput(input);
		TrancodeOutPut output = new TrancodeOutPut();
		output.setUrl("http://" + AppId + ".vod2.myqcloud.com/" + id + "/f20.hls");// 需要校验
		output.setContainer("hls");// 需要校验
		e.setOutput(output);
		processTaskList.add(e);
		return rb;
	}

	/**
	 * 暂停销售
	 * 
	 * @param liveid
	 * @param pid
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void SuspendLiveProduct(int liveid, String pid, Integer sellerid)
			throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":暂停销售:pid:" + pid);
		String token = AuthCallServiceV2.createToken(sellerid, "app");
		SuspendLiveProductBean suspendliveproductBean = new SuspendLiveProductBean();
		SuspendLiveProductCall suspendliveproductCall = new SuspendLiveProductCall();
		suspendliveproductBean.setLiveId(liveid);
		suspendliveproductBean.setProductId(pid);
		suspendliveproductCall.setData(false, suspendliveproductBean);
		String head = "accesstoken=" + token;
		suspendliveproductCall.addHeader("ymt-pars", head);
		suspendliveproductCall.callService();
	}

	/**
	 * 恢复销售
	 * 
	 * @param liveid
	 * @param pid
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void ResumeLiveProduct(int liveid, String pid, Integer sellerid)
			throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":恢复销售:pid:" + pid);
		String token = AuthCallServiceV2.createToken(sellerid, "app");
		ResumeLiveProductBean resumeliveproductBean = new ResumeLiveProductBean();
		ResumeLiveProductCall resumeliveproductCall = new ResumeLiveProductCall();
		resumeliveproductBean.setLiveId(liveid);
		List<String> ProductIdList = new ArrayList<>();
		ProductIdList.add(pid);
		resumeliveproductBean.setProductIds(ProductIdList);
		resumeliveproductCall.setData(resumeliveproductBean);
		String head = "accesstoken=" + token;
		resumeliveproductCall.addHeader("ymt-pars", head);
		resumeliveproductCall.callService();
	}

//	/**
//	 * 查询直播商品列表
//	 * @param liveid
//	 * @return
//	 * @throws ClientProtocolException
//	 * @throws IOException
//	 */
//	public static JsonArray QueryLiveProductIdList(Integer liveid) throws ClientProtocolException, IOException {
//		QueryLiveProductIdListBean queryliveproductidlistBean=new QueryLiveProductIdListBean();
//		QueryLiveProductIdListCall queryliveproductidlistCall=new QueryLiveProductIdListCall();
//		queryliveproductidlistBean.setLiveId(liveid);
//		queryliveproductidlistCall.setData(queryliveproductidlistBean);
//		queryliveproductidlistCall.callService();		
//		return queryliveproductidlistCall.getProductList();
//	}
	/**
	 * 查询直播中商品列表
	 * 
	 * @param liveid
	 * @param userid
	 * @param tabindex
	 * @return
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static LiveProductListCall GetLiveProductList(Integer liveid, Integer sellerid, int tabindex)
			throws InterruptedException, ClientProtocolException, IOException {
		String token = AuthCallServiceV2.createToken(sellerid, "app");
		LiveProductListBean liveproductlistBean = new LiveProductListBean();
		LiveProductListCall liveproductlistCall = new LiveProductListCall();
		liveproductlistBean.setLiveId(liveid);
		liveproductlistBean.setTabIndex(tabindex);
		liveproductlistCall.setData(liveproductlistBean);
		String head = "accesstoken=" + token;
		liveproductlistCall.addHeader("ymt-pars", head);
		liveproductlistCall.callService();
		return liveproductlistCall;
	}

	/**
	 * 自动关闭直播(直接改数据库)
	 * 
	 * @param uid
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void AutoCloseLiveRoomByUserId(Integer uid)
			throws ClientProtocolException, IOException, InterruptedException {
		LiveRoomShop2cnWapper liveRoomWapper = new LiveRoomShop2cnWapper();
		List<LiveRoom> roomlist = liveRoomWapper.selectRunningLiveRoomByUserid(uid);
		for (LiveRoom liveRoom : roomlist) {
			liveRoomWapper.updateLiveRoomTime(YMTDateUtil.getBeforeOrNextMunite(-100),
					YMTDateUtil.getBeforeOrNextMunite(-50), YMTDateUtil.getBeforeOrNextMunite(-50), liveRoom.getId());
			CloseSellerLiveBean closesellerliveBean = new CloseSellerLiveBean();
			CloseSellerLiveCall closesellerliveCall = new CloseSellerLiveCall();
			closesellerliveBean.setLiveId(liveRoom.getId());
//			closesellerliveBean.setUserId(uid);
			if (liveRoom.getLivePush() == 0) {
				Logger.comment(liveRoom.getId() + ":已有直播自动关闭");
				closesellerliveBean.setCloseType("CloseLive");
			} else if (liveRoom.getLivePush() == 1) {
				Logger.comment(liveRoom.getId() + "已有直播自动结束");
				closesellerliveBean.setCloseType("FinishLive");
			}
			closesellerliveCall.setData(false, closesellerliveBean);
			String token = AuthCallServiceV2.createToken(uid, "app");
			String head = "accesstoken=" + token;
			closesellerliveCall.addHeader("ymt-pars", head);
			closesellerliveCall.callService();
			if (closesellerliveCall.getStatusCode() != 200 || !"200".equals(closesellerliveCall.getString("code"))) {
				// 失败的话直接改成action -1
				liveRoomWapper.updateLiveRoomAction(liveRoom.getId(), -1);
//				SysCloseLive(liveRoom.getId());
			}
		}
	}

	/**
	 * 强制主播上线,会修改开始时间
	 * 
	 * @param liveid
	 * @param uid
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void SellerLiveOnline(Integer liveid, Integer uid) throws ClientProtocolException, IOException {
		UpdateLiveTimeToStart(liveid);
		Logger.comment(liveid + ":主播上线:uid:" + uid);
		SellerLiveOnlineBean sellerliveonlineBean = new SellerLiveOnlineBean();
		SellerLiveOnlineCall sellerliveonlineCall = new SellerLiveOnlineCall();
		sellerliveonlineBean.setLiveId(liveid);
		sellerliveonlineCall.setData(sellerliveonlineBean);

		String token = AuthCallServiceV2.createToken(uid, "app");
		String head = "accesstoken=" + token;
		sellerliveonlineCall.addHeader("ymt-pars", head);

		sellerliveonlineCall.callService();
	}

	public static void SellerLiveOnline(Integer liveid, Integer uid, boolean ut)
			throws ClientProtocolException, IOException {
		if (ut) {
			UpdateLiveTimeToStart(liveid);
		}
		Logger.comment(liveid + ":主播上线:uid:" + uid);
		SellerLiveOnlineBean sellerliveonlineBean = new SellerLiveOnlineBean();
		SellerLiveOnlineCall sellerliveonlineCall = new SellerLiveOnlineCall();
		sellerliveonlineBean.setLiveId(liveid);
		sellerliveonlineCall.setData(sellerliveonlineBean);
		String token = AuthCallServiceV2.createToken(uid, "app");
		String head = "accesstoken=" + token;
		sellerliveonlineCall.addHeader("ymt-pars", head);
		sellerliveonlineCall.callService();
	}

	/**
	 * 强制更新数据库时间为当前时间之前5分钟(开始)
	 * 
	 * @param liveid
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void UpdateLiveTimeToStart(Integer liveid) throws ClientProtocolException, IOException {
		String time = YMTDateUtil.getBeforeOrNextMunite(-10);
		Logger.comment("更新开始时间:" + time);

		new LiveRoomShop2cnWapper().updateLiveRoomTime(time, null, null, liveid);
	}

	/**
	 * 发放优惠券
	 * 
	 * @param liveid
	 * @param batchCode
	 * @param sellerId
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void SendCoupon(Integer liveid, String batchCode, Integer sellerId)
			throws ClientProtocolException, IOException {
		SendCouponBean sendcouponBean = new SendCouponBean();
		SendCouponCall sendcouponCall = new SendCouponCall();
		sendcouponBean.setLiveId(liveid);
		sendcouponBean.setBatchCode(batchCode);
		sendcouponCall.setData(sendcouponBean);
		String token = AuthCallServiceV2.createToken(sellerId, "app");
		String head = "accesstoken=" + token;
		sendcouponCall.addHeader("ymt-pars", head);
		sendcouponCall.callService();
	}

	// #####################
	/**
	 * 初始化tx回调退断流信息
	 * 
	 * @param liveid
	 * @param event_type 1推流 0断流
	 * @return
	 * @throws Exception 真实报文
	 *                   {"app":"12808.livepush.myqcloud.com","appId":"1255315550","appname":"live","channel_id":"12808_SIT1_235","errcode":1,"errmsg":"recv
	 *                   rtmp
	 *                   deleteStream","event_type":0,"node":"100.67.177.19","requestId":"781eaafd69764f7fa9f97d08e5c612cb","sequence":"1418989456100673054","sign":"5bbc740031c41e44d62c48058e4de804","stream_id":"12808_SIT1_235","stream_param":"txSecret=97f7e30da2082d7eefcad89371c04181&txTime=59F08C74","t":"1509006840","update_time":0,"user_ip":"114.80.87.210"}
	 */
	public static StreamLiveNotifyBean initStreamLiveNotifyBean(Integer id, int event_type) throws Exception {
		String sequence = TestDataManager.getRandomNum(9);
		Long ltime = YMTDateUtil.getDate().getTime();
		String time = Long.toHexString(ltime);
		StreamLiveNotifyBean streamlivenotifyBean = new StreamLiveNotifyBean();
		streamlivenotifyBean.setChannel_id(BizId + "_" + EnvSetup.getEnv() + "_" + id);
		streamlivenotifyBean.setStream_id(BizId + "_" + EnvSetup.getEnv() + "_" + id);
		streamlivenotifyBean.setT(String.valueOf(YMTDateUtil.getDate().getTime()));
		streamlivenotifyBean.setEvent_type(event_type);
		streamlivenotifyBean.setSign(TestDataManager.getMD5(id.toString().getBytes()));
		streamlivenotifyBean.setApp(BizId + ".livepush.myqcloud.com");
		streamlivenotifyBean.setNode(TestDataManager.getRandomIp());
		streamlivenotifyBean.setSequence(sequence);
		streamlivenotifyBean.setSign(TestDataManager.getMD5((LiveApiKey + ltime.toString()).getBytes()));
		streamlivenotifyBean.setT(ltime.toString());
		streamlivenotifyBean.setStream_param("bizid="
				+ BizId + "&txSecret=" + TestDataManager.getMD5((LiveRoomCallService.LiveAntiKey
						+ LiveRoomCallService.BizId + "_" + id + Long.toHexString(ltime)).getBytes())
				+ "&txTime=" + time);
		return streamlivenotifyBean;
	}

	/**
	 * 初始化录播回调报文
	 * 
	 * @param id
	 * @param event_type
	 * @return
	 * @throws Exception
	 */
	public static RecLiveNotifyBean initRecLiveNotifyBean(Integer id, int event_type) throws Exception {
		String sequence = TestDataManager.getRandomNum(9);
		Long ltime = YMTDateUtil.getDate().getTime();
		String time = Long.toHexString(ltime);
		RecLiveNotifyBean reclivenotifyBean = new RecLiveNotifyBean();
		reclivenotifyBean.setChannel_id(BizId + "_" + EnvSetup.getEnv() + "_" + id);
		reclivenotifyBean.setStream_id(BizId + "_" + EnvSetup.getEnv() + "_" + id);
		reclivenotifyBean.setT(String.valueOf(YMTDateUtil.getDate().getTime()));
		reclivenotifyBean.setEvent_type(event_type);
		reclivenotifyBean.setSign(TestDataManager.getMD5(id.toString().getBytes()));
		reclivenotifyBean.setSign(TestDataManager.getMD5((LiveApiKey + ltime.toString()).getBytes()));
		reclivenotifyBean.setT(ltime.toString());
		reclivenotifyBean.setStream_param("bizid="
				+ BizId + "&txSecret=" + TestDataManager.getMD5((LiveRoomCallService.LiveAntiKey
						+ LiveRoomCallService.BizId + "_" + id + Long.toHexString(ltime)).getBytes())
				+ "&txTime=" + time);
		reclivenotifyBean.setAppid(AppId);
		reclivenotifyBean.setDuration("120");
		reclivenotifyBean.setEnd_time(String.valueOf(YMTDateUtil.getBeforeOrNextDate(-1).getTime()).substring(0, 10));
		reclivenotifyBean.setFile_format("hls");
		reclivenotifyBean.setFile_id(TestDataManager.getRandomUUID().toString().replaceAll("-", ""));
		reclivenotifyBean.setFile_size("20000");
		reclivenotifyBean.setStart_time(String.valueOf(YMTDateUtil.getBeforeOrNextDate(0).getTime()).substring(0, 10));
		String video_id = AppId + "_" + TestDataManager.getRandomNum(27);
		reclivenotifyBean.setVideo_url("http://" + AppId + ".vod2.myqcloud.com/" + video_id + "/playlist.m3u8");
		reclivenotifyBean.setVideo_id(video_id);
		return reclivenotifyBean;
	}

	/**
	 * 第三方通知
	 * 
	 * @param streamlivenotifyBean
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void LiveNotify(LiveNotifyBean streamlivenotifyBean) throws ClientProtocolException, IOException {
		Logger.comment("第三方通知:Event_type:" + streamlivenotifyBean.getEvent_type());
		LiveNotifyCall livenotifyCall = new LiveNotifyCall();
		livenotifyCall.setData(false, streamlivenotifyBean);
		livenotifyCall.callService();
	}

	// ############# livebuyer ################
	/**
	 * 加入群
	 * 
	 * @param liveid
	 * @param userId
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void enterGroup(int liveid, int userId)
			throws InterruptedException, ClientProtocolException, IOException {
		Logger.comment(liveid + ":加入群:uid:" + userId);
		EnterGroupBean enterGroupBean = new EnterGroupBean();
		EnterGroupCall enterGroupCall = new EnterGroupCall();
		enterGroupBean.setLiveId(liveid);
		enterGroupBean.setUserId(userId);
		enterGroupCall.setData(false, enterGroupBean);
		enterGroupCall.callService();
	}

	/**
	 * 退出群
	 * 
	 * @param liveid
	 * @param userId
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void quitGroup(int liveid, int userId)
			throws InterruptedException, ClientProtocolException, IOException {
		Logger.comment(liveid + ":退出群:uid:" + userId);
		QuitGroupBean quitGroupBean = new QuitGroupBean();
		QuitGroupCall quitGroupCall = new QuitGroupCall();
		quitGroupBean.setLiveId(liveid);
		quitGroupBean.setUserId(userId);
		quitGroupCall.setData(false, quitGroupBean);
		quitGroupCall.callService();
	}

	/**
	 * 增加/获取比心数
	 * 
	 * @param liveid
	 * @param userId
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static int getLikeCount(int liveid, int userId, int count) throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":比心:count:" + count);
		LikeBean likeBean = new LikeBean();
		LikeCall likeCall = new LikeCall();
		likeBean.setLiveId(liveid);
		likeBean.setUserId(userId);
		likeBean.setCount(count);
		likeCall.setData(false, likeBean);
		likeCall.callService();
		return likeCall.getJsonObject().get("data").getAsJsonObject().get("totalCount").getAsInt();
	}

	/**
	 * 同步直播排名
	 * 
	 * @param sellerid
	 * @param liveId
	 * @param topnum       排名
	 * @param LiveCategory * 1箱包手袋榜 2 珠宝配饰榜 3 潮流服饰榜 10美容家居榜
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void SyncLivesellerTop(Integer sellerid, Integer liveId, Integer topnum, Integer LiveCategory)
			throws ClientProtocolException, IOException {
		SyncLiveSellerTopBean synclivesellertopBean = new SyncLiveSellerTopBean();
		SyncLiveSellerTopCall synclivesellertopCall = new SyncLiveSellerTopCall();
		synclivesellertopBean.setSellerId(sellerid);
		synclivesellertopBean.setLiveId(liveId);
		synclivesellertopBean.setTopNum(topnum);
		synclivesellertopBean.setLiveCategory(LiveCategory);
		synclivesellertopCall.setData(synclivesellertopBean);
		synclivesellertopCall.callService();
	}

	/**
	 * 设置直播热度
	 * 
	 * @param liveId
	 * @param Heat
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void setLiveHeat(Integer liveId, Double Heat) throws ClientProtocolException, IOException {
		SyncLiveHeatBean syncliveheatBean = new SyncLiveHeatBean();
		SyncLiveHeatCall syncliveheatCall = new SyncLiveHeatCall();
		syncliveheatBean.setHeat(Heat);
		syncliveheatBean.setLiveId(liveId);
		syncliveheatCall.setData(syncliveheatBean);
		syncliveheatCall.callService();
	}

	/**
	 * 创建聊天室groupid
	 * 
	 * @param groupname
	 * @return groupid
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String CreateGroup(String groupname, Integer sellerid) throws ClientProtocolException, IOException {
		Logger.comment("创建群:groupname:" + groupname);
		CreateGroupBean creategroupBean = new CreateGroupBean();
		CreateGroupCall creategroupCall = new CreateGroupCall();
		creategroupBean.setGroupName(groupname);
		creategroupBean.setSellerId(sellerid);
		creategroupCall.setData(creategroupBean);
		creategroupCall.callService();
		if (!creategroupCall.getString("code").equals("200")) {
			creategroupCall.callService();
		}
		return creategroupCall.getString("data");
	}

	/**
	 * 订阅直播App
	 * 
	 * @param liveid
	 * @param userid
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void SubscribeLive(Integer liveid, Integer userid)
			throws ClientProtocolException, IOException, InterruptedException {
		SubscribeBean subscribeBean = new SubscribeBean();
		SubscribeCall subscribeCall = new SubscribeCall();
		subscribeBean.setLiveId(liveid);
		subscribeBean.setUserId(userid);
		subscribeCall.setData(subscribeBean);
		subscribeCall.callService();
	}

	/**
	 * 订阅直播WxApp
	 *
	 * @param liveid
	 * @param userid
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void SubscribeLiveWx(Integer liveid, Integer userid)
			throws ClientProtocolException, IOException, InterruptedException {
		SubscribeBean subscribeBean = new SubscribeBean();
		SubscribeCall subscribeCall = new SubscribeCall();
		subscribeBean.setLiveId(liveid);
		subscribeBean.setUserId(userid);
		subscribeBean.setSource(1);
		subscribeBean.setFormId(YMTDateUtil.getBeforeOrNextDay(0));
		subscribeCall.setData(subscribeBean);
		subscribeCall.callService();
	}

	/**
	 * 取消订阅直播App
	 * 
	 * @param liveid
	 * @param userid
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void UnSubscribeLive(Integer liveid, Integer userid)
			throws ClientProtocolException, IOException, InterruptedException {
		UnSubscribeBean unsubscribeBean = new UnSubscribeBean();
		UnSubscribeCall unsubscribeCall = new UnSubscribeCall();
		unsubscribeBean.setLiveId(liveid);
		unsubscribeBean.setUserId(userid);
		unsubscribeCall.setData(unsubscribeBean);
		unsubscribeCall.callService();
	}

	/**
	 * 取消订阅直播WxApp
	 *
	 * @param liveid
	 * @param userid
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void UnSubscribeLiveWx(Integer liveid, Integer userid)
			throws ClientProtocolException, IOException, InterruptedException {
		UnSubscribeBean unsubscribeBean = new UnSubscribeBean();
		UnSubscribeCall unsubscribeCall = new UnSubscribeCall();
		unsubscribeBean.setLiveId(liveid);
		unsubscribeBean.setUserId(userid);
		unsubscribeCall.setData(unsubscribeBean);
		unsubscribeCall.callService();
	}

	/**
	 * livestatus 2 livePhase
	 * 
	 * @param livestatus
	 * @return
	 */
	public static Integer getLivePhase(Integer livestatus) {
		int livePhase = 0;
		if (livestatus == 0 || livestatus == 1) {
			livePhase = 10;
		} else if (livestatus == 2 || livestatus == 3) {
			livePhase = 20;
		} else {
			livePhase = 30;
		}
		return livePhase;
	}

	/**
	 * 解除直播禁言
	 * 
	 * @param liveid
	 * @param sellerid
	 * @param uid
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void CancelShutUpInLiveRoom(Integer liveid, Integer sellerid, Integer uid)
			throws ClientProtocolException, IOException {
		CancelShutUpInLiveRoomBean CancelShutUpInLiveRoomBean = new CancelShutUpInLiveRoomBean();
		CancelShutUpInLiveRoomCall CancelShutUpInLiveRoomCall = new CancelShutUpInLiveRoomCall();
		CancelShutUpInLiveRoomBean.setLiveId(liveid);
		CancelShutUpInLiveRoomBean.setSellerId(sellerid);
		CancelShutUpInLiveRoomBean.setUserId(uid);
		CancelShutUpInLiveRoomCall.setData(CancelShutUpInLiveRoomBean);
		CancelShutUpInLiveRoomCall.callService();
	}

	/**
	 * 直播间禁言
	 * 
	 * @param liveid
	 * @param sellerid
	 * @param uid
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void ShutUpInLiveRoom(Integer liveid, Integer sellerid, Integer uid)
			throws ClientProtocolException, IOException {
		ShutUpInLiveRoomBean shutupinliveroomBean = new ShutUpInLiveRoomBean();
		ShutUpInLiveRoomCall shutupinliveroomCall = new ShutUpInLiveRoomCall();
		shutupinliveroomBean.setLiveId(liveid);
		shutupinliveroomBean.setSellerId(sellerid);
		shutupinliveroomBean.setUserId(uid);
		shutupinliveroomCall.setData(shutupinliveroomBean);
		shutupinliveroomCall.callService();
	}

	/**
	 * 发送消息
	 * 
	 * @param groupid
	 * @param content
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public static SendGroupMessageCall SendMsg(String groupid, String content)
			throws ClientProtocolException, IOException {
		SendGroupMessageBean sendgroupmessageBean = new SendGroupMessageBean();
		SendGroupMessageCall sendgroupmessageCall = new SendGroupMessageCall();
		sendgroupmessageBean.setGroupId(groupid);
		sendgroupmessageBean.setContent(content);
		sendgroupmessageCall.setData(sendgroupmessageBean);
		sendgroupmessageCall.callService();
		return sendgroupmessageCall;
	}
}
